package io.github.project_travel_mate.destinations.description;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import io.github.project_travel_mate.R;
import objects.QaItem;

public class QaAdapter extends RecyclerView.Adapter<QaAdapter.QaVH> {

    private final List<QaItem> mList;        // ← 改名
    private final LayoutInflater mInflater;  // 顺手也改成 mInflater

    public QaAdapter(Context ctx, List<QaItem> list) {
        mList = list;
        mInflater = LayoutInflater.from(ctx);
    }

    @NonNull
    @Override
    public QaVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = mInflater.inflate(R.layout.activity_qa, parent, false);
        return new QaVH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull QaVH holder, int position) {
        QaItem item = mList.get(position);
        holder.question.setText(item.getQuestion());
        holder.answer.setText(item.getAnswer());
        holder.icon.setImageResource(R.drawable.ic_smart_toy);

        boolean isExpanded = item.isExpanded();
        holder.answer.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
        holder.itemView.setOnClickListener(v -> {
            item.setExpanded(!item.isExpanded());
            notifyItemChanged(position);
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class QaVH extends RecyclerView.ViewHolder {
        TextView question, answer;
        ImageView icon;
        QaVH(View itemView) {
            super(itemView);
            question = itemView.findViewById(R.id.tv_question);
            answer   = itemView.findViewById(R.id.tv_answer);
            icon     = itemView.findViewById(R.id.iv_icon);
        }
    }
}